/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.staff.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.staff.entity.Staff;
import com.pitop.staff.service.StaffSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 员工信息 控制器
 *
 * @author pi-top
 */
@RestController
@RequestMapping("/staff")
@Slf4j
@Api(value = "员工信息控制器", description = "员工信息控制器")
public class StaffCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private StaffSV staffSV;


    /**
     * 查询Staff一个详情信息
     *
     * @param id
     * @param code 唯一编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Staff一个详情信息", notes = "查询Staff一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public Staff load(java.lang.Long id, java.lang.String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Staff staff = staffSV.load(id, code);
        log.info(JSON.toJSONString(staff));
        return staff;
    }


    /**
     * 根据条件id查询Staff一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Staff一个详情信息", notes = "查询Staff一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public Staff loadById(@PathVariable java.lang.Long id) {
        if (id == null) {
            return null;
        }
        Staff staff = staffSV.loadById(id);
        log.info(JSON.toJSONString(staff));
        return staff;
    }

    /**
     * 根据条件code查询Staff一个详情信息
     *
     * @param code 唯一编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Staff一个详情信息", notes = "查询Staff一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "唯一编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public Staff loadByCode(@PathVariable java.lang.String code) {
        if (code == null) {
            return null;
        }
        Staff staff = staffSV.loadByCode(code);
        log.info(JSON.toJSONString(staff));
        return staff;
    }


    /**
     * 查询Staff信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Staff信息集合", notes = "查询Staff信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "jobnumber", value = "工号", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态 入职 hire，离职 leave", paramType = "query"),
            @ApiImplicitParam(name = "hiredate", value = "入职时间", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<Staff> list(@RequestBody @ApiIgnore Staff staff, Integer curPage, Integer pageSize) {
        Page<Staff> page = new Page<Staff>(pageSize, curPage);
        List<Staff> staffs = staffSV.list(staff, page.genRowStart(), page.getPageSize());
        int total = staffSV.count(staff);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return staffs;
    }


    /**
     * 查询Staff信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Staff信息集合", notes = "查询Staff信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<Staff> listByPk(java.lang.Long id, java.lang.String code, Integer curPage, Integer pageSize) {
        Page<Staff> page = new Page<Staff>(pageSize, curPage);
        List<Staff> staffs = staffSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = staffSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return staffs;
    }

    /**
     * 统计Staff信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计Staff信息数量根据主键", notes = " 统计Staff信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(java.lang.Long id, java.lang.String code) {
        return staffSV.count(id, code);
    }

    /**
     * 统计Staff信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计Staff信息数量", notes = "统计Staff信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "jobnumber", value = "工号", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态 入职 hire，离职 leave", paramType = "query"),
            @ApiImplicitParam(name = "hiredate", value = "入职时间", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody Staff staff) {
        return staffSV.count(staff);
    }


    /**
     * 创建Staff
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Staff", notes = "创建Staff")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "jobnumber", value = "工号", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "hiredate", value = "入职时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public Staff build(@RequestBody @ApiIgnore Staff staff) {
        staffSV.save(staff);
        return staff;
    }


    /**
     * 修改Staff
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Staff", notes = "修改Staff")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "jobnumber", value = "工号", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "工厂编码", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "电话", paramType = "query"),
            @ApiImplicitParam(name = "gender", value = "性别", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态 入职 hire，离职 leave", paramType = "query"),
            @ApiImplicitParam(name = "hiredate", value = "入职时间", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public Staff modify(@RequestBody @ApiIgnore Staff staff) {
        if (staff == null) {
            return new Staff();
        }
        staffSV.modify(staff);
        return staff;
    }

    /**
     * 删除Staff
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Staff", notes = "删除Staff")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "唯一编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(java.lang.Long id, java.lang.String code) {
        staffSV.delete(id, code);
        return BeanRet.create(true, "删除Staff成功");
    }

    @ApiOperation(value = "根据员工号和姓名模糊查询", notes = "根据员工号和姓名模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNumber", value = "jobNumber", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "name", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "factoryCode", paramType = "query"),
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query")
    })
    @RequestMapping(value = "/listByJobNumberAndNameAndFactoryCode", method = RequestMethod.GET)
    @ResponseBody
    public List<Staff> listByJobNumberAndNameAndFactoryCode(String jobNumber, String name, String factoryCode, @ApiIgnore Page<Staff> page) {
        List<Staff> staffs = staffSV.listByJobNumberAndNameAndFactoryCode(jobNumber, name, factoryCode, page.genRowStart(), page.getPageSize());
        //时间格式转换
        staffs = JSON.parseArray(JSON.toJSONString(staffs, SerializerFeature.WriteDateUseDateFormat), Staff.class);
        int total = staffSV.countByJobNumberAndNameAndFactroyCode(jobNumber, name, factoryCode);
        page.setVoList(staffs);
        page.setTotalRow(total);
        return staffs;
    }

    @ApiOperation(value = "根据员工号和姓名模糊查询的总数", notes = "根据员工号和姓名模糊查询的总数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNumber", value = "jobNumber", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "name", paramType = "query")
    })
    @RequestMapping(value = "/countByJobNumberAndNameAndFactroyCode", method = RequestMethod.GET)
    @ResponseBody
    public Integer countByJobNumberAndNameAndFactroyCode(String jobNumber, String name, String factoryCode) {
        int total = staffSV.countByJobNumberAndNameAndFactroyCode(jobNumber, name, factoryCode);
        return total;
    }

    @ApiOperation(value = "根据员工号查询staff", notes = "根据员工号查询staff")
    @ApiImplicitParam(name = "jobNumber", value = "jobNumber", paramType = "query")
    @RequestMapping(value = "/loadStaffByJobNumber", method = RequestMethod.POST)
    @ResponseBody
    public Staff loadStaffByJobNumber(String jobNumber) {
        return staffSV.loadStaffByJobNumber(jobNumber);
    }

    @ApiOperation(value = "根据员工号查询staff", notes = "根据员工号查询staff")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNumber", value = "jobNumber", paramType = "query"),
            @ApiImplicitParam(name = "factoryCode", value = "factoryCode", paramType = "query")
    })
    @RequestMapping(value = "/loadStaffByJobNumberAndFactoryCode", method = RequestMethod.POST)
    @ResponseBody
    public Staff loadStaffByJobNumberAndFactoryCode(String jobNumber, String factoryCode) {
        return staffSV.loadStaffByJobNumberAndFactoryCode(jobNumber, factoryCode);
    }

}
